<?php
namespace Api\Controller;
use Think\Controller;
class IndexController extends Controller{
    public function index()
    {
        echo 'API';

    }

    /*添加好友*/
    public function AddFriend()
    {
        if (!$_SESSION['islogin'] == 2) {
            $this->error('请登录后再操作', 'http://localhost/Qzone/index.php/Home/Users/Login');
            // $this->redirect('Home/Users/Login');
        } else {
            if (I('post.friend_id')) {
                $User = M('users');
                $Frd = M('friend');
                if (I('post.friend_id') != null && I('post.friend_id') > 0) {
                    $data['Friend_Id'] = I('post.friend_id');
                    $data['User_Id'] = $_SESSION['user_id'];
                    $data_user = I('post.friend_id');
                    //判断是否查找到ID
                    if ($User->where("user_id=$data_user")->find()) {
                        //判断是否为自己ID或已添加
                        if ($data['Friend_Id'] == $_SESSION['user_id'] || $Frd->where($data)->find()) {
                            echo BaseController::ApiError("添加失败！");
                            exit;
                        } else {

                            $Frd->add($data);
                            echo BaseController::ApiSuccess("添加成功！");
                            exit;
                        }
                    } else {
                        echo BaseController::ApiError("好友ID不存在！");
                        exit;
                    }
                }
            }
        }
    }

    /*我的好友*/
    public function  MyFriend()
    {

        if (IS_POST) {
            if (!empty($_SESSION['user_id'])) {
                $user_id = $_SESSION['user_id'];
                $sql = ("select DISTINCT u.user_name from users u join friend f on u.user_id=f.friend_id where friend_id in(select friend_id from friend where (user_id='$user_id'))");
                $result = M()->query($sql);
                if (!empty($result)) {
                    echo BaseController::ApiSuccess($result);
                    exit;
                } else {
                    echo BaseController::ApiSuccess('您还没添加好友');
                    exit;
                }
            } else {
                echo BaseController::ApiError('没登录！');
                exit;
            }

        }
    }

    /*查找好友*/
    public function  look_f()
    {
        if (!$_SESSION['islogin'] == 2) {
            $this->error('请登录后再操作', 'http://localhost/Qzone/index.php/Home/Users/Login');
        } else {
            if (I('post.friend_name')) {
                $user_id = $_SESSION['user_id'];
                $friend = I('friend_name');
                $sql = ("select * from (select DISTINCT u.user_name from users u join friend f on u.user_id=f.friend_id where friend_id in(select friend_id from friend where (user_id='$user_id'))) p where user_name like '%$friend%'");
                $result = M()->query($sql);
                if (!empty($result)) {
                    echo BaseController::ApiSuccess($result);
                    exit;
                } else {
                    echo BaseController::ApiError('搜索无结果！');
                    exit;
                }
            }
        }
    }

    /* 删除好友*/
    public function DeFriend()
    {
        if (!$_SESSION['islogin'] == 2) {
            $this->error('请登录后再操作', 'http://localhost/Qzone/index.php/Home/Users/Login');
        } else {
            if (isset($_POST['friend_id'])) {
                $DeFriend['User_Id'] = $_SESSION['user_id'];
                $DeFriend['Friend_Id'] = $_POST['friend_id'];
                $Frd = M('friend');
                if ($Frd->where($DeFriend)->find()) {

                    if ($Frd->where($DeFriend)->delete()) {
                        echo BaseController::ApiSuccess('删除成功！');
                        exit;
                    } else {
                        echo BaseController::ApiError('删除失败！');
                        exit;
                    }
                } else {
                    echo BaseController::ApiError('好友不存在！');
                    exit;
                }

            }
        }
    }

}